/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.latticeware.xtalk.stack;

import com.latticeware.xtalk.app.LatticeCard;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;



/**
 *
 * @author sfisque
 */
public class AnswerDialog
extends Dialog
{
    protected static String STR_OK = "OK";
    
    
    protected Label promptLabel;
    
    protected List<Button> buttons;
    
    protected int which = 0;
    
    
    public AnswerDialog()
    {
        super( LatticeCard.getInstance().getWindow(), null, true );
        
        setUndecorated( true );
        setResizable( false );
        
        promptLabel = new Label( "What is it?" );
        add( promptLabel );
        
        ButtonListener bl = new ButtonListener();
        
        Button button = new Button( STR_OK );
        button.addActionListener( bl );
        add( button, BorderLayout.SOUTH );
        
        button = new Button( "" );
        button.addActionListener( bl );
        add( button, BorderLayout.SOUTH );
        button.setVisible( false );
        
        button = new Button( "" );
        button.addActionListener( bl );
        add( button, BorderLayout.SOUTH );
        button.setVisible( false );        
    }


    public String getPrompt()
    {
        return promptLabel.getText();
    }


    public void setPrompt(String prompt)
    {
        promptLabel.setText( prompt );
    }
    
    
    public void setChoices( String[] labels )
    {
        if( labels == null || labels.length == 0 )
        {
            buttons.get( 0 ).setLabel( STR_OK );
        }
        else
        {
            for( int i = 0; i < 3; i ++ )
            {
                if( i < labels.length )
                {
                    buttons.get( i ).setLabel( labels[ i ] );
                    buttons.get( i ).setVisible( true );
                }
                else
                {
                    buttons.get( i ).setVisible( false );
                }
            }
        }
    }
    

    public String getAnswer()
    {
        return buttons.get( which ).getLabel();
    }
    
    class ButtonListener
    implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            AnswerDialog.this.setVisible( false );
            
            which = buttons.indexOf( e.getSource() );
        }
        
    }
}
